import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:example/base/custom_widget.dart';
import 'package:example/config/color_config.dart';
import 'package:example/widget/my_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

/// 自定义AppBar
class MyNewNavBar extends CustomWidget<NavBarController>
    implements PreferredSizeWidget {
  final Color? backgroundColor;
  final Color? backImgColor;
  final String title;
  final String backImg;
  final String leftName; //左边名称
  final String actionImg; //右边图片
  final String actionName;
  int messageCount;
  final Color actionColor;
  final VoidCallback? onLeftTap;
  final VoidCallback? onRightTap;
  final bool isBack;
  final bool isGradient;
  final LinearGradient gradient;
  final Color titleColor;
  final List<Widget>? lastChildren;
  final double? actionFontSize;
  final Widget Function()? buildTitle;
  final SystemUiOverlayStyle statusBarTheme;

  MyNewNavBar(
      {Key? key,
      this.backgroundColor = ColorConfig.bgGrey,
      NavBarController? controller,
      String centerTitle = "",
      this.title = '',
      this.actionName = '',
      this.leftName = '',
      this.actionImg = '',
      this.messageCount = 0,
      this.actionColor = Colors.white,
      this.backImg = 'assets/svg/ic_arrow_left.svg',
      this.backImgColor = Colors.grey,
      this.onLeftTap,
      this.onRightTap,
      this.isBack = true,
      this.isGradient = false,
      this.gradient = const LinearGradient(
        colors: [Color(0xFF1548E1), Color(0xFF4289F4)],
        begin: Alignment.centerLeft,
        end: Alignment.centerRight,
      ),
      this.lastChildren,
      this.titleColor = ColorConfig.text,
      this.actionFontSize,
      this.buildTitle,
      this.statusBarTheme = SystemUiOverlayStyle.dark})
      : super(
          NavBarController(centerTitle: centerTitle),
          externalController: controller,
          key: key,
        ) {
    if (controller != null) {
      controller.centerTitle = controller.centerTitle ?? centerTitle;
    }
  }

  @override
  Widget initView(BuildContext context, NavBarController controller) {
    return AnnotatedRegion(
        child: _content(context, controller), value: statusBarTheme);
  }

  _content(BuildContext context, NavBarController controller) {
    Color _backgroundColor;
    if (backgroundColor == null) {
      _backgroundColor = Colors.white;
    } else {
      _backgroundColor = backgroundColor!;
    }

    var back = Opacity(
      opacity: isBack ? 1 : 0,
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          if (onLeftTap != null) {
            onLeftTap?.call();
          } else {
            FocusScope.of(context).unfocus();
            Get.back();
          }
        },
        child: Container(
          height: 48.r,
          padding: EdgeInsets.symmetric(horizontal: 16.r),
          child: MyImage(
            path: backImg,
            color: backImgColor,
            width: 20.r,
            rotate: 0,
          ),
        ),
      ),
    );

    back = Opacity(
      opacity: leftName.isEmpty ? 0 : 1,
      child: Theme(
        data: Theme.of(context).copyWith(
            buttonTheme: ButtonThemeData(
          padding: EdgeInsets.symmetric(horizontal: 16.0.r),
          minWidth: 60.0.r,
        )),

        child: ElevatedButton(
          onPressed: onLeftTap,

          child: Text(
            leftName,
            key: const Key('leftName'),
            style: TextStyle(
              fontSize: actionFontSize,
              color: actionColor,
              fontWeight: FontWeight.normal,
            ),
          ),
        ),
      ),
    );

    var action = actionName.isNotEmpty
        ? Positioned(
            right: 0.0,
            child: Theme(
              data: Theme.of(context).copyWith(
                  buttonTheme: ButtonThemeData(
                padding: EdgeInsets.symmetric(horizontal: 16.0.r),
                minWidth: 60.0.r,
              )),
              child: ElevatedButton(
                onPressed: onRightTap,
                child: Text(
                  actionName,
                  key: const Key('actionName'),
                  style: TextStyle(
                    fontSize: actionFontSize,
                    color: actionColor,
                    fontWeight: FontWeight.normal,
                  ),
                ),
              ),
            ),
          )
        : SizedBox.shrink();

    action = actionImg.isNotEmpty
        ? Positioned(
            right: 0.0,
            child: GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                if (onRightTap != null) {
                  onRightTap?.call();
                }
              },
              child: Container(
                height: 48.r,
                padding: EdgeInsets.symmetric(horizontal: 16.r),
                child: MyImage(
                  path: actionImg,
                  color: actionColor,
                  width: 20.r,
                  rotate: 0,
                ),
              ),
            ),
          )
        : SizedBox.shrink();

    var last = lastChildren ?? [];
    return Material(
      color: Colors.transparent,
      child: Container(
        decoration: BoxDecoration(
          color: isGradient ? null : _backgroundColor,
          gradient: isGradient ? gradient : null,
        ),
        child: SafeArea(
          left: false,
          bottom: false,
          child: Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              Semantics(
                namesRoute: true,
                header: true,
                child: Container(
                    height: 48.r,
                    margin: EdgeInsets.symmetric(horizontal: 48.0.r),
                    alignment: controller.centerTitle?.isEmpty == true
                        ? Alignment.centerLeft
                        : Alignment.center,
                    width: double.infinity,
                    child: buildTitle?.call() ??
                        Text(
                          title.isEmpty ? controller.centerTitle ?? "" : title,
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            fontSize: 18.sp,
                            color: titleColor,
                          ),
                        )),
              ),
              back,
              action,
              Positioned(
                right: 0,
                child: ConstrainedBox(
                  constraints: BoxConstraints(
                    maxHeight: 48.r,
                  ),
                  child: Row(
                    children: last,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  // // 未读小红点
  // _circleRedStateView() {
  //   var show = messageCount == 0 ? false : true;
  //   var messText =
  //       messageCount.toString().length > 2 ? '99+' : messageCount.toString();
  //   return Positioned(
  //     top: 0.w,
  //     right: 5.w,
  //     //个数显示
  //     child: PrimaryBtn(
  //       borderRadius: BorderRadius.all(Radius.circular(10.w)),
  //       height: 20.w,
  //       width: 20.w,
  //       background: show ? Colors.red : Colors.transparent,
  //       text: messText,
  //       textColor: show ? Colors.white : Colors.transparent,
  //       fontSize: messText.length > 2 ? 9.sp : 11.sp,
  //     ),
  //   );
  // }

  @override
  Size get preferredSize => Size.fromHeight(48.0.r);
}

class NavBarController extends GetxController {
  String? centerTitle;

  NavBarController({this.centerTitle});

  setCenterTitle(String centerTitle) {
    this.centerTitle = centerTitle;
    update();
  }
}

// // 红点
// class circleRedStateView extends StatelessWidget {
//   int? messageCount = 0;

//   circleRedStateView(
//       {this.messageCount = 0,
//       Key? key})
//       : super(key: key);

//   @override
//   Widget build(BuildContext context) {
//     var show = messageCount == 0 ? false : true;
//     var messText =
//         messageCount.toString().length > 2 ? '99+' : messageCount.toString();
//     return Positioned(
//       top: 0.w,
//       right: 5.w,
//       //个数显示
//       child: PrimaryBtn(
//         borderRadius: BorderRadius.all(Radius.circular(10.w)),
//         height: 20.w,
//         width: 20.w,
//         background: show ? Colors.red : Colors.transparent,
//         text: messText,
//         textColor: show ? Colors.white : Colors.transparent,
//         fontSize: messText.length > 2 ? 9.sp : 11.sp,
//       ),
//     );
//   }
// }
